package ar.activity;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import ar.Location.Localization.LocalizationHelper;

public class BaseActivity extends Activity {

	public TextView tvWhatScreen = null;
	public ImageView imgViewEN = null;
	public ImageView imgViewVI = null;
	public static Resources res = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		res = getResources();
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.window_title);

		tvWhatScreen = (TextView) findViewById(R.id.tvTitleBar);
		
		imgViewVI = (ImageView) findViewById(R.id.imgViewVietnameseFlag);
		imgViewEN = (ImageView) findViewById(R.id.imgViewEnglishFlag);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		if(LocalizationHelper.isEnglish) {
			imgViewEN.setEnabled(false);
			imgViewEN.setAlpha(96);
			imgViewVI.setEnabled(true);
			imgViewVI.setAlpha(255);
			LocalizationHelper.changeLocaleSetting(getApplicationContext(),
					"en");
		} else {
			imgViewEN.setEnabled(true);
			imgViewEN.setAlpha(255);
			imgViewVI.setEnabled(false);
			imgViewVI.setAlpha(96);
			LocalizationHelper.changeLocaleSetting(getApplicationContext(),
					"vi");
		}

		initEventChangeLocaleSetting();
		
		onUpdateUI();
	}

	public void initEventChangeLocaleSetting() {
		imgViewVI.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LocalizationHelper.changeLocaleSetting(getApplicationContext(),
						"vi");
				LocalizationHelper.isEnglish = false;
				imgViewEN.setEnabled(true);
				imgViewEN.setAlpha(255);
				imgViewVI.setEnabled(false);
				imgViewVI.setAlpha(96);
				onUpdateUI();
			}
		});

		imgViewEN.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LocalizationHelper.changeLocaleSetting(getApplicationContext(),
						"en");
				LocalizationHelper.isEnglish = true;
				imgViewEN.setEnabled(false);
				imgViewEN.setAlpha(96);
				imgViewVI.setEnabled(true);
				imgViewVI.setAlpha(255);
				onUpdateUI();
			}
		});
	}
	
	protected void onUpdateUI() {
		return;
	}

	protected void releaseImageResource(int id) {
		try {
			Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(id))
					.getBitmap();
			if (bmp != null) {
				bmp.recycle();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
